package day08;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class test011 {
    WebDriver driver;

    @BeforeSuite
    public void prepare(){
        driver = new ChromeDriver();
        driver.get("https://www.baidu.com");
        driver.manage().window().maximize();//最大化
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));//隐式等待
    }
    @Test
    public void test001() throws InterruptedException {
        System.out.println("验证百度搜索功能后，回退到首页");
        driver.findElement(By.id("kw")).sendKeys("Java自动化测试");
        driver.findElement(By.id("su")).click();
        Thread.sleep(5000);//等待5s
        driver.navigate().back();//浏览器的回退按钮
    }
    @Test
    public void test02() throws InterruptedException {
        System.out.println("定位百度首页上所有的a标签,获取每个a标签的href属性");
        List<WebElement> links = driver.findElements(By.tagName("a"));
        for (WebElement link : links) {
            System.out.println(link.getText());//获取文本信息
            System.out.println(link.getAttribute("href"));//获取属性的值
            System.out.println("------");
        }
        Thread.sleep(5000);
    }
    @Test
    public void test03() throws InterruptedException {
        System.out.println("显式等待");
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5),Duration.ofMillis(500));//显式等待
        WebElement webElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("新闻")));
        System.out.println(webElement.getAttribute("href"));

        System.out.println("强制等待");
        TimeUnit.SECONDS.sleep(5);//等待3秒
        TimeUnit.MILLISECONDS.sleep(3000);//等待3秒
        TimeUnit.MINUTES.sleep(1);//等待60秒

    }
    @AfterSuite
    public void clear(){
        driver.quit();//退出浏览器
    }
}
